//
//  myapplicationFirstViewController.m
//  myapplication
//
//  Created by Paolo Marcolli on 08/01/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "myapplicationFirstViewController.h"
#import "articlemanager.h"
#import "article.h"

@implementation myapplicationFirstViewController
@synthesize articles;
@synthesize txt_price,txt_article,tbl_View,navigationItem;

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [self.articles.list count]; 
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    if (editingStyle ==  UITableViewCellEditingStyleDelete) 
    {
        //add code here for when you hit delete
        
        //[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
        //Delete the object from the table.
        //[self.tbl_View deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } 
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell.
    UIImage *cellImage = [UIImage imageNamed:@"allegato.png"];
    cell.imageView.image = cellImage;
    
    article *item = [self.articles.list objectAtIndex: [indexPath row]];
    
    cell.textLabel.text = [item description];
    
    NSString *valuta = @"€ ";
    NSNumber *valore1=[item price];
    NSString *valore = [NSString stringWithFormat:@"%@", valore1];
    
    NSString *price_formatted = [valuta stringByAppendingString:valore]; 
    cell.detailTextLabel.text = price_formatted;
    return cell;    
    
} 


// Costumize group header text.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"Lista della spesa :";
}

// Costumize footer text.
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 
{
	return @"Totale :";
}

// Touch down button
- (IBAction) add
{
    // Add event. (NSString *)n description:(NSString *)d url:(NSString *)u price:(NSNumber *)p ;
    
    //NSNumber  *aNum = [NSNumber numberWithInteger: [[txt_price text] integerValue]];
    NSNumber  *aNum = [NSNumber numberWithDouble: [[txt_price text] doubleValue]];
    
    [self.articles add : @"000" description:txt_article.text url:@"" price: aNum];
    
    [self.tbl_View reloadData];
}

// Touch down button
- (IBAction) navigationEdit
{
    // Add event. (NSString *)n description:(NSString *)d url:(NSString *)u price:(NSNumber *)p ;
    [self.articles add : @"000" description:txt_article.text url:@"" price:0 ];
    
    [self.tbl_View reloadData];
}
// Touch down button
- (IBAction) navigationBack
{
    // Add event. (NSString *)n description:(NSString *)d url:(NSString *)u price:(NSNumber *)p ;
    [self.articles add : @"000" description:txt_article.text url:@"" price:0 ];
    
    [self.tbl_View reloadData];
}
// Touch down button
- (IBAction) navigationAdd
{
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    articles = [articlemanager new];
    [self.articles load];
    
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.navigationItem.title = @"Test";
    UIBarButtonItem *addButton  =
    [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
     target: self
     action: @selector(addNewSegment)];
    self.navigationItem.rightBarButtonItem = addButton;
    addButton =nil;
    //self.navigationItem.rightBarButtonItem= self.
}

-(IBAction)addNewSegment 
{
    // here you can access the object which triggered the method
    // for example you can check the tag value
    
    //NSLog(@"the tag value is: %d", [sender tag]);
    [self add];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.articles=nil;
    self.txt_price=nil;
    self.txt_article=nil;
    self.tbl_View=nil;
    self.navigationItem=nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}


@end
